<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('流程类型')" prop="flowType">
            <dict-select
              v-model="form.flowType"
              dictionary="flow_type"
              :placeholder="$t('流程类型')"
            />
          </el-form-item>
        </el-col>
        <el-col v-if="form.flowType === 'deal'" :span="12">
          <el-form-item :label="$t('项目类型')" prop="dealType">
            <dict-select
              v-model="form.dealType"
              dictionary="deal_type"
              :placeholder="$t('项目类型')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('流程名称')" prop="flowName">
            <el-input v-model="form.flowName" :placeholder="$t('流程名称')" show-word-limit maxlength="32" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('路由地址')" prop="path">
            <el-input v-model="form.path" :placeholder="$t('路由地址')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('排序')" prop="sort">
            <el-input v-model="form.sort" :placeholder="$t('排序')" show-word-limit />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('菜单编辑权限')" prop="menuEdit">
            <el-input v-model="form.menuEdit" :placeholder="$t('菜单编辑权限')" show-word-limit />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('流程logo')" prop="logoInfo">
            <UploadAvatar :logo-info="form.logoInfo" @appendData="uploadImg($event)" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="act:workbenchConfig" />
    </div>
  </div>
</template>

<script>
    import { saveWfWorkbenchConfig, getWfWorkbenchConfig, uploadAvatar } from '@/api/act/workbenchConfig/wfWorkbenchConfig'
    import UploadAvatar from '@/components/Avatar/uploadAvatar.vue'
    export default {
      components: { UploadAvatar },
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  flowType: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  dealType: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  flowName: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  path: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  sort: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  menuEdit: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  logoInfo: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
                }
            }
        },
        created() {
            this.getDicts('flow_type').then(response => {
                this.flowTypeOptions = response.data
            })
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getWfWorkbenchConfig(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 上传图片
            uploadImg(data) {
              const formData = new FormData()
              formData.append('avatarfile', data)
              formData.append('id', this.form.id)
              uploadAvatar(formData).then(response => {
                if (response.code === 200) {
                  this.form.logoInfo = response.msg
                  this.msgSuccess(this.$t('修改成功'))
                } else {
                  this.msgError(response.msg)
                }
              })
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    flowType: undefined,
                    dealType: undefined,
                    flowName: undefined,
                    path: undefined,
                    sort: undefined,
                    menuEdit: undefined,
                    logoInfo: undefined,
                    remark: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveWfWorkbenchConfig(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           }
        }
    }
</script>
